package com.xiaoyu;


import com.xiaoyu.model.Account;
import com.xiaoyu.model.Student;
import com.xiaoyu.service.AccountService;
import com.xiaoyu.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Api(tags = "学生信息接口")
@RestController
//@CrossOrigin(origins = "http://39.101.65.116:8080")
@CrossOrigin(origins = "http://localhost:8080")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private AccountService accountService;

    @ApiOperation("学生信息查询接口")
    @GetMapping("/studentInfo")
    public Student studentInfo(HttpSession session){
        Account a= (Account) session.getAttribute("loginAccount");
        if (a!=null){
            Student student =new Student();
            student.setAid(a.getId());
            Student s=studentService.findOneStudent(student);
            session.setAttribute("student",s);
            return s;
        }
        return null;
    }

    @ApiOperation("学生列表查询接口")
    @PostMapping("/studentList")
    public List<Student> studentList(@RequestBody Student student){
        return studentService.allStudent(student);
    }

    @ApiOperation("学生添加接口")
    @PutMapping("/student/{nameFirstCode}")
    public String addStudent(@RequestBody Student student,
                             @PathVariable String nameFirstCode){
        String accountName;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy");
        //设置入学时年龄
        Integer attendSchoolTime= Integer.parseInt(sdf.format(student.getAttendSchoolTime()));
        Integer sbirthday= Integer.parseInt(sdf.format(student.getSbirthday()));
        student.setSage(attendSchoolTime-sbirthday);
        //账户信息
        Account account=new Account();
        accountName=nameFirstCode+
                sdf.format(student.getAttendSchoolTime())+
                student.getSage()+System.currentTimeMillis()%10000;
        account.setAccountName(accountName);
        //添加账户
        accountService.addAccount(account);
        //添加学生
        if(studentService.addStudent(student,account)){
            return "success";
        }
        return "error";
    }

    @ApiOperation("学生删除接口")
    @DeleteMapping("/student")
    public String deleteStudent(@RequestBody Student student){
        Account a=new Account();
        a.setId(student.getAid());
        accountService.deleteAccount(a);
        if(studentService.deleteStudent(student))
            return "success";
        return "error";
    }

    @ApiOperation("学生毕业接口")
    @PostMapping("/graduation")
    public String graduation(@RequestBody String gradList){
        int i= gradList.indexOf('[');
        int j= gradList.indexOf(']');
        //字符串转int类型List,l:字符串类型List,qList:int类型List
        List<String> l= Arrays.asList(gradList.substring(i+1,j).split(","));
        Student s=new Student();
        s.setIsGraduation(1);
        s.setGraduationTime(new Date());
        Account a=new Account();
        a.setState(0);
        for(String grad :l){
            s.setId(Integer.parseInt(grad));
            a.setId(studentService.findOneStudent(s).getAid());
            accountService.updateAccount(a);
            studentService.updateStudent(s);
        }
        return "success";
    }

    @ApiOperation("学生修改接口")
    @PostMapping("/student")
    public String updateStudent(@RequestBody Student student){
        if(studentService.updateStudent(student))
            return "success";
        return "error";
    }
}
